package pool.test.client;

import java.util.Random;

import pool.ThreadPool;
import pool.model.IThread;

public class Client extends Thread {

	private ThreadPool pool;
	private String clientName;
	private int min = 2000;
	private int max = 6000;
	
	public Client(String clientName) {
		this.clientName = clientName;
		pool = ThreadPool.getInstance();
		
//		System.out.println("*** client " + clientName + " created ***");
	}

	public void run() {
		try {
		IThread t = pool.getThread(this);
			
			if (t != null) {
				System.out.println(">>>client " + clientName +"<< is processing..");
				Thread.sleep(min + (int)(Math.random() * ((max - min) + 1)));
				pool.releaseThread(t);
			}else{
				System.err.println("Thread is null!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String getClientName(){
		return clientName;
	}
}